﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace RXY_Mall.Core
{
    public class XmlHelper
    {
        /// <summary>
        /// 读取某节点名称的一个值
        /// </summary>
        /// <param name="filePath">服务器上的绝对路径</param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static string ReadNodes(string filePath, string xPath)
        {
            var doc = new XmlDocument();
            doc.Load(filePath);
            if (doc.DocumentElement != null)
            {
                XmlNode xn = doc.DocumentElement.SelectSingleNode(xPath);
                return xn.InnerText;
            }
            return null;
        }
        /// <summary>
        /// 读取某节点名称的多个值
        /// </summary>
        /// <param name="filePath">服务器上的绝对路径</param>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public static List<string> ReadSeveralNodes(string filePath, string xPath)
        {
            var res = new List<string>();

            var doc = new XmlDocument();
            doc.Load(filePath);
            if (doc.DocumentElement != null)
            {
                XmlNodeList xn = doc.DocumentElement.SelectNodes(xPath);
                if (xn != null && xn.Count > 0)
                {
                    for (int i = 0; i < xn.Count; i++)
                    {
                        XmlNode n = xn[i];
                        res.Add(n.InnerText);
                    }
                }

                return res;
            }
            return null;
        }

        public static string ReadNodes(XmlDocument doc, string xPath)
        {
            if (doc.DocumentElement != null)
            {
                XmlNode xn = doc.DocumentElement.SelectSingleNode(xPath);
                return xn.InnerText;
            }
            return null;
        }
    }
}

